package crypto.reference;

import crypto.process.CompleteBlock;
import crypto.process.DeleteEmptySpace;
import crypto.process.Matrix;
import crypto.process.Sustitution;
import crypto.util.Util;
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;

/**
 * 
DOCUMENT ME!
 *
 * @author gato
 */
public class References {
    
  private int     sustitutionChar;
  private int     sustitutionVariation;
  private int     completeBlock;
  private int[][] deleteMatrix;
  private String  deletePosition;
  private int[] matrixPositions;
  private int posFinal;
  
  private String stringReference;
  
  public References(){
      sustitutionChar = 0;
      sustitutionVariation = 0;
      completeBlock = 0;
      deletePosition = "";
      stringReference = "";
      matrixPositions = new int[2];
      deleteMatrix = new int[4][2];
      posFinal = 0;
  }
  
  public References( Matrix matrix, CompleteBlock complete, 
          DeleteEmptySpace delete, Sustitution sustitution){
      
      sustitutionChar = sustitution.getAddChar();
      sustitutionVariation = sustitution.getVariation();
      deleteMatrix = delete.getMatrix();
      deletePosition = delete.getPosition();
      completeBlock = complete.getBlocks();
      matrixPositions = matrix.getPositionMap();
      stringReference = "";
      posFinal = complete.getOriginalSize();
  }
  
  public String unhideKeysInText(String textE, String reference) {

      Hide hide = new Hide();
      
      String[] ref = hide.obtainReferences(textE, reference);
      Logger logger = Logger.getLogger("crypto");

      posFinal = Util.convertB36Decimal( ref[14] );
      logger.info("PosFinal: "+posFinal);
      deletePosition = ref[13];
      logger.info("DeletePosition: "+deletePosition);
      deleteMatrix[3][1] = Util.convertB36Decimal( ref[12] );
      logger.info("DeleteMatrix00: "+deleteMatrix[3][1]);
      deleteMatrix[3][0] = Util.convertB36Decimal( ref[11] );
      logger.info("DeleteMatrix00: "+deleteMatrix[3][0]);
      deleteMatrix[2][1] = Util.convertB36Decimal( ref[10] );
      logger.info("DeleteMatrix00: "+deleteMatrix[2][1]);
      deleteMatrix[2][0] = Util.convertB36Decimal( ref[9] );
      logger.info("DeleteMatrix00: "+deleteMatrix[2][0]);
      deleteMatrix[1][1] = Util.convertB36Decimal( ref[8] );
      logger.info("DeleteMatrix00: "+deleteMatrix[1][1]);
      deleteMatrix[1][0] = Util.convertB36Decimal( ref[7] );
      logger.info("DeleteMatrix00: "+deleteMatrix[1][0]);
      deleteMatrix[0][1] = Util.convertB36Decimal( ref[6] );
      logger.info("DeleteMatrix00: "+deleteMatrix[0][1]);
      deleteMatrix[0][0] = Util.convertB36Decimal( ref[5] );
      logger.info("DeleteMatrix00: "+deleteMatrix[0][0]);
      completeBlock = Util.convertB36Decimal( ref[4] );
      logger.info("CompleteBlock: "+completeBlock);
      sustitutionVariation = Util.convertB36Decimal( ref[3] );
      logger.info("SustitutionVariation: "+sustitutionVariation);
      sustitutionChar = Util.convertB36Decimal( ref[2] );
      logger.info("SustitutionChar: "+sustitutionChar);
      matrixPositions[1] = Util.convertB36Decimal( ref[1] );
      logger.info("Matrix1: "+matrixPositions[1]);
      matrixPositions[0] = Util.convertB36Decimal( ref[0] );
      logger.info("Matrix0: "+matrixPositions[0]);
      
      return ref[14];
  }   
  
    public String hideKeysInText(String text) {
        int textSize = text.length();
        Hide hide = new Hide();
        Logger logger = Logger.getLogger("crypto");
        
        logger.info(text);
        String key = Util.convertDecimalB36( getMatrixPositions()[0] , 1);
        text = hide.hideIndividualKey(text, key);
        stringReference += hide.getReference();
        logger.info("MatrixPos0: "+text);
        
        key = Util.convertDecimalB36( getMatrixPositions()[1] , 1);
        text = hide.hideIndividualKey(text, key);
        stringReference += hide.getReference();
        logger.info("MatrixPos1: "+text);
        
        key = Util.convertDecimalB36( sustitutionChar , 1);
        text = hide.hideIndividualKey(text, key);
        stringReference += hide.getReference();
        logger.info("SustitutionChar: "+text);
        
        key = Util.convertDecimalB36( sustitutionVariation , 1);
        text = hide.hideIndividualKey(text, key);
        stringReference += hide.getReference();
        logger.info("SustitutionVariation: "+text);
        
        key = Util.convertDecimalB36( completeBlock , 1);
        text = hide.hideIndividualKey(text, key);
        stringReference += hide.getReference();
        logger.info("CompleteBlock: "+text);
        
        key = Util.convertDecimalB36( deleteMatrix[0][0] , 1);
        text = hide.hideIndividualKey(text, key);
        stringReference += hide.getReference();
        logger.info("DeleteMatrix00"+text);

        key = Util.convertDecimalB36( deleteMatrix[0][1] , 1);
        text = hide.hideIndividualKey(text, key);
        stringReference += hide.getReference();
        logger.info("DeleteMatrix01"+text);

        key = Util.convertDecimalB36( deleteMatrix[1][0] , 1);
        text = hide.hideIndividualKey(text, key);
        stringReference += hide.getReference();
        logger.info("DeleteMatrix10"+text);

        key = Util.convertDecimalB36( deleteMatrix[1][1] , 1);
        text = hide.hideIndividualKey(text, key);
        stringReference += hide.getReference();
        logger.info("DeleteMatrix11"+text);

        key = Util.convertDecimalB36( deleteMatrix[2][0] , 1);
        text = hide.hideIndividualKey(text, key);
        stringReference += hide.getReference();
        logger.info("DeleteMatrix20"+text);

        key = Util.convertDecimalB36( deleteMatrix[2][1] , 1);
        text = hide.hideIndividualKey(text, key);
        stringReference += hide.getReference();
        logger.info("DeleteMatrix21"+text);

        key = Util.convertDecimalB36( deleteMatrix[3][0] , 1);
        text = hide.hideIndividualKey(text, key);
        stringReference += hide.getReference();
        logger.info("DeleteMatrix30"+text);

        key = Util.convertDecimalB36( deleteMatrix[3][1] , 1);
        text = hide.hideIndividualKey(text, key);
        stringReference += hide.getReference();
        logger.info("DeleteMatrix31"+text);

        text = hide.hideIndividualKey(text, deletePosition);
        stringReference += hide.getReference();
        logger.info("DeletePositions"+text);

        key = Util.convertDecimalB36( posFinal , 1);
        text = hide.hideIndividualKey(text, key);
        stringReference += hide.getReference();
        logger.info("PosFinal: "+text);

        logger.info(stringReference);
        
        return text;
    }

  /**
   * DOCUMENT ME!
   *
   * @return DOCUMENT ME!
   */
  public int getSustitutionChar() {
    return sustitutionChar;
  }

  /**
   * DOCUMENT ME!
   *
   * @return DOCUMENT ME!
   */
  public int getSustitutionVariation() {
    return sustitutionVariation;
  }

  /**
   * DOCUMENT ME!
   *
   * @param sustitutionChar DOCUMENT ME!
   */
  public void setSustitutionChar(int sustitutionChar) {
    this.sustitutionChar = sustitutionChar;
  }

  /**
   * DOCUMENT ME!
   *
   * @param sustitutionVariation DOCUMENT ME!
   */
  public void setSustitutionVariation(int sustitutionVariation) {
    this.sustitutionVariation = sustitutionVariation;
  }

  /**
   * DOCUMENT ME!
   *
   * @return DOCUMENT ME!
   */
  public int getCompleteBlock() {
    return completeBlock;
  }

  /**
   * DOCUMENT ME!
   *
   * @param deleteBlock DOCUMENT ME!
   */
  public void setDeleteBlock(int completeBlock) {
    this.completeBlock = completeBlock;
  }

  /**
   * DOCUMENT ME!
   *
   * @return DOCUMENT ME!
   */
  public int[][] getDeleteMatrix() {
    return deleteMatrix;
  }

  /**
   * DOCUMENT ME!
   *
   * @param deleteMatrix DOCUMENT ME!
   */
  public void setDeleteMatrix(int[][] deleteMatrix) {
    this.deleteMatrix = deleteMatrix;
  }

  /**
   * DOCUMENT ME!
   *
   * @return DOCUMENT ME!
   */
  public String getDeletePosition() {
    return deletePosition;
  }

  /**
   * DOCUMENT ME!
   *
   * @param deletePosition DOCUMENT ME!
   */
  public void setDeletePosition(String deletePosition) {
    this.deletePosition = deletePosition;
  }

    public String getStringReference() {
        return stringReference;
    }

    public int getPosFinal() {
        return posFinal;
    }

    public void setPosFinal(int posFinal) {
        this.posFinal = posFinal;
    }

    public int[] getMatrixPositions() {
        return matrixPositions;
    }

}
